<?php
$loader->requireOnce("includes/Datasource_sql.class.php");

class PatientSurgery extends ORDataObject {
	var $patient_surgery_id = 	'';
	var $patient_id 	    = 	'';
	var $surgical_name	    =	'';
	var $surgery_date   	=	'';
	var $updated            = '';
	var $note               = '';
	var $type               =   '';
	
	var $_table = 'patient_surgery';
	var $_key = 'patient_surgery_id';
	var $_internalName = 'PatientSurgery';
	var $_foreignKeyList=array(
		'patient_id'=>"patient"
	);
	
	const SUR_TYPE_SMALL	    =	'small';
	const SUR_TYPE_MEDIUM		=	'medium';
	const SUR_TYPE_BIG	        =	'big';
	
	function PatientSurgery($db='') {
		parent::ORDataObject($db);
	}
	
	function getPatientSergeries($patient_id){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "patient_surgery_id as id, surgical_name, type as surgery_type, surgery_date
 							, note, updated",
			'from' 		=> "$this->_table", 
			'where' 	=> "patient_id = ".(int)$patient_id
			);
					
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
    
    public function getSurgeryTypeList(){
    	$list	=	array(
    		self::SUR_TYPE_SMALL	=> 	L10nHelper::_("Small surgery"),
    		self::SUR_TYPE_MEDIUM	=>	L10nHelper::_("Medium sugery"),
    		self::SUR_TYPE_BIG		=>	L10nHelper::_("Big surgery"));
        return $list;
    }    
	
    public function checkDuplicated($patientid,$surgical_name,$surgery_date,$surgery_type){
		$ds =& new Datasource_sql();
        $sql = array(
            'cols'      => "patient_surgery_id  as id",
            'from'      => "$this->_table",
        	'where'		=> "patient_id=$patientid AND surgical_name='$surgical_name' 
                            AND surgery_date='$surgery_date' AND type='$surgery_type'"        	                
            );
        $ds->setup($this->_db,$sql);
        $res=$ds->toArray();
      	if(!empty($res))
      		return $res[0]['id'];
      	return 0;
	}
    
	public function setData($patientid,$surgical_name,$surgery_date,$surgery_type,$note,$updated=''){
		if($updated!='')
			$date=$updated;
		else
			$date=date('Y-m-d');
		$data = array (
				'patient_id' 		=> 	$patientid,
				'surgical_name'     => 	$surgical_name,
		        'surgery_date'      => 	$surgery_date,
				'type'		=> 	$surgery_type,
				'note'			    =>	$note,
				'updated'			=>	$date
			);		
		$this->populateArray($data);
	}
	 
	public function getUpdated(){
       return $this->getLocalizedDate('updated');
	}
	
	public function getSurgeryDate(){
        return $this->getLocalizedDate('surgery_date');
	}
	
    public function getSurgicalName(){
        return $this->surgical_name;   
    }
    
	public function getSurgeryType(){
		$list	=	self::getSurgeryTypeList();
		if(!array_key_exists($this->type, $list)){
			return '';
		}
		return $list[$this->type];
	}
	
	public function getNote(){
		return $this->note;
	}
	
	public function deleteSurgery($id){
		$sql	=	"DELETE from  $this->_table where $this->_key=".(int)$id;
		$this->_execute($sql);
	}
	
	public function setId($id){
		$this->patient_surgery_id	=	$id;
	}
	
	public function getId(){
		return $this->patient_surgery_id;
	}
}
?>